/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.tool.sphinx.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.SystemColor;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

public class EditorWindow extends JFrame {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  final static private String TITLE = "Sphinx Editor - ";

  BorderLayout borderLayout1 = new BorderLayout();
  JPanel statusPane = new JPanel();
  Border border1;
  JLabel statusMsg = new JLabel();
  BorderLayout borderLayout2 = new BorderLayout();
  JMenuBar mainmenu = new JMenuBar();
  JMenu fileItem = new JMenu();
  JMenu editItem = new JMenu();
  JMenu helpItem = new JMenu();
  JMenuItem helpAboutItem = new JMenuItem();
  JMenuItem fileExitItem = new JMenuItem();
  JMenuItem fileOpenItem = new JMenuItem();
  JMenuItem fileOpenFromClipboardItem = new JMenuItem();
  JMenu fileNewItem = new JMenu();
  JMenu windowItem = new JMenu();
  JMenuItem windowNewEditorItem = new JMenuItem();
  JMenuItem editCutItem = new JMenuItem();
  JMenuItem editCopyItem = new JMenuItem();
  JMenuItem editPasteItem = new JMenuItem();
  JMenuItem editDeleteItem = new JMenuItem();
  JFileChooser openSavePane = new JFileChooser();
  JMenuItem fileSaveItem = new JMenuItem();
  JMenuItem fileSaveAsItem = new JMenuItem();
  JMenuItem fileSaveToClipboardItem = new JMenuItem();
  JMenuItem fileCloseItem = new JMenuItem();
  JMenuItem fileCloseAllItem = new JMenuItem();
  JMenuItem editSelectAllItem = new JMenuItem();

  final static public String EMPTY_MSG = " ";
  JMenuItem editUndoItem = new JMenuItem();
  JMenuItem editRedoItem = new JMenuItem();

  public EditorWindow() {
    try {
      jbInit();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.white,Color.white,new Color(134, 134, 134),new Color(93, 93, 93));
    Container contentPane = this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.getContentPane().setBackground(SystemColor.inactiveCaption);
    this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    this.setJMenuBar(mainmenu);
    this.setTitle( TITLE );
    statusPane.setBorder(border1);
    statusPane.setLayout(borderLayout2);
    statusMsg.setText( EMPTY_MSG );
    fileItem.setMnemonic('F');
    fileItem.setText("File");
    editItem.setMnemonic('E');
    editItem.setText("Edit");
    helpItem.setMnemonic('H');
    helpItem.setText("Help");
    helpAboutItem.setText("About");
    helpAboutItem.setMnemonic('A');
    fileExitItem.setMnemonic('X');
    fileExitItem.setText("Exit");
    fileExitItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(115, java.awt.event.KeyEvent.ALT_MASK, false));
    fileOpenItem.setMnemonic('O');
    fileOpenItem.setText("Open...");
    fileOpenItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(79, java.awt.event.KeyEvent.CTRL_MASK, false));
    fileOpenFromClipboardItem.setMnemonic('C');
    fileOpenFromClipboardItem.setText("Open from Clipboard");
    fileNewItem.setMnemonic('N');
    fileNewItem.setText("New");
    windowItem.setMnemonic('W');
    windowItem.setText("Window");
    windowNewEditorItem.setMnemonic('N');
    windowNewEditorItem.setText("New editor");
    windowNewEditorItem.setEnabled( false );
    editCutItem.setEnabled(false);
    editCutItem.setMnemonic('T');
    editCutItem.setText("Cut");
    editCutItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(88, java.awt.event.KeyEvent.CTRL_MASK, false));
    editCopyItem.setEnabled(false);
    editCopyItem.setMnemonic('C');
    editCopyItem.setText("Copy");
    editCopyItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(67, java.awt.event.KeyEvent.CTRL_MASK, false));
    editPasteItem.setMnemonic('P');
    editPasteItem.setText("Paste");
    editPasteItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(86, java.awt.event.KeyEvent.CTRL_MASK, false));
    editPasteItem.setEnabled(false);
    editDeleteItem.setEnabled(false);
    editDeleteItem.setMnemonic('D');
    editDeleteItem.setText("Delete");
    editDeleteItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(127, 0, false));
    fileSaveItem.setEnabled(false);
    fileSaveItem.setMnemonic('S');
    fileSaveItem.setText("Save");
    fileSaveItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(83, java.awt.event.KeyEvent.CTRL_MASK, false));
    fileSaveAsItem.setEnabled(false);
    fileSaveAsItem.setMnemonic('A');
    fileSaveAsItem.setText("Save As...");
    fileSaveToClipboardItem.setEnabled(false);
    fileSaveToClipboardItem.setMnemonic('V');
    fileSaveToClipboardItem.setText("Save to Clipboard");
    fileCloseItem.setEnabled(false);
    fileCloseItem.setText("Close");
    fileCloseItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(115, java.awt.event.KeyEvent.CTRL_MASK, false));
    fileCloseAllItem.setText("Close All");
    editSelectAllItem.setMnemonic('A');
    editSelectAllItem.setText("Select All");
    editSelectAllItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(65, java.awt.event.KeyEvent.CTRL_MASK, false));
    editSelectAllItem.setEnabled( false );
    editUndoItem.setEnabled(false);
    editUndoItem.setText("Undo");
    editUndoItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(90, java.awt.event.KeyEvent.CTRL_MASK, false));
    editUndoItem.setMnemonic('U');
    editRedoItem.setEnabled(false);
    editRedoItem.setText("Redo");
    editRedoItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(90, java.awt.event.KeyEvent.CTRL_MASK | java.awt.event.KeyEvent.SHIFT_MASK, false));
    editRedoItem.setMnemonic('R');
    contentPane.add(statusPane, BorderLayout.SOUTH);
    statusPane.add(statusMsg, BorderLayout.CENTER);
    mainmenu.add(fileItem);
    mainmenu.add(editItem);
    mainmenu.add(windowItem);
//    mainmenu.add( Box.createHorizontalGlue() );
    mainmenu.add(helpItem);
    helpItem.add(helpAboutItem);
    fileItem.add(fileNewItem);
    fileItem.add(fileOpenItem);
    fileItem.add(fileOpenFromClipboardItem);
    fileItem.addSeparator();
    fileItem.add(fileCloseItem);
    fileItem.add(fileCloseAllItem);
    fileItem.addSeparator();
    fileItem.add(fileSaveItem);
    fileItem.add(fileSaveAsItem);
    fileItem.add(fileSaveToClipboardItem);
    fileItem.addSeparator();
    fileItem.add(fileExitItem);
    windowItem.add(windowNewEditorItem);
    editItem.add(editUndoItem);
    editItem.add(editRedoItem);
    editItem.addSeparator();
    editItem.add(editCutItem);
    editItem.add(editCopyItem);
    editItem.add(editPasteItem);
    editItem.add(editDeleteItem);
    editItem.addSeparator();
    editItem.add(editSelectAllItem);

		ImageIcon frameIcon =
			new ImageIcon(EditorWindow.class.getResource("/self/amigo/images/sphinxIcon.gif"));
		setIconImage(frameIcon.getImage());
  }

  public File openDialog() {
    int resp = openSavePane.showOpenDialog( null );
    if ( resp == JFileChooser.APPROVE_OPTION )
      return openSavePane.getSelectedFile();
    return null;
  }

  public File saveDialog() {
    int resp = openSavePane.showSaveDialog( null );
    if ( resp == JFileChooser.APPROVE_OPTION )
      return openSavePane.getSelectedFile();
    return null;
  }

  public void setTitleCaption( String title ) {
    setTitle( TITLE + (title == null ? "": title) );
  }

  public void promptInfoMessage( String title, Object msg ) {
    JOptionPane.showMessageDialog( this, msg, title, JOptionPane.INFORMATION_MESSAGE );
  }

  public void promptErrorMessage( String title, Object msg ) {
    JOptionPane.showMessageDialog( this, msg, title, JOptionPane.ERROR_MESSAGE );
  }

  public int promptForChoice( String title, String msg, int options ) {
    return JOptionPane.showConfirmDialog( this, msg, title, options );
  }

  public void resetMenu( java.util.List menusToInstall ) {
    mainmenu.removeAll();
    mainmenu.add(fileItem);
    mainmenu.add(editItem);

    int max = menusToInstall.size();
    for ( int cntr = 0, ind = 4; cntr < max; cntr++, ind++ ) {
      JMenu newMenu = (JMenu) menusToInstall.get( 0 );
      mainmenu.add( newMenu );
    }

    mainmenu.add(windowItem);
    mainmenu.add(helpItem);
  }
}